package org.game.msgBuilder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.constant.smallkey.PlayerDataSmallKey;
import org.game.db.bean.Hero;
import org.game.db.bean.PlayerData;
import org.game.db.proxy.bean.HeroProxy;
import org.game.db.vo.EquipmentVO;
import org.game.db.vo.SkinVO;
import org.game.protobuf.s2c.S2CHeroMsg;
import org.game.protobuf.s2c.S2CHeroMsg.AngeSkillInfo;
import org.game.protobuf.s2c.S2CHeroMsg.HeroInfo;
import org.game.protobuf.s2c.S2CHeroMsg.HeroInfoList;
import org.game.protobuf.s2c.S2CShareMsg.AttributeInfo;
import org.game.protobuf.s2c.S2CShareMsg.ChangeType;

import com.google.common.collect.Maps;
import com.sojoys.artifact.tools.ToolMap;

/**
 * @author : DengYing
 * @CreateDate : 2017年8月15日 下午4:21:17
 * @Description ：Please describe this document
 */
@SuppressWarnings("unchecked")
public class HeroMsgBuilder {

	public static final String HERO_ID = "id";
	public static final String HERO_CID = "cid";
	public static final String HERO_LVL = "lel";
	public static final String HERO_EXP = "exp";
	public static final String HERO_ADVANCED_LVL = "adv_lv";
	public static final String HERO_ATTR = "attr";
	public static final String HERO_EQUIPMENT = "equip";
	public static final String HELP_FIGHT = "hf";

	public static HeroInfo createHeroInfo(ChangeType type,Hero hero){
		return createHeroInfo(type,hero,0);
	}
	
	public static HeroInfo createHeroInfo(ChangeType type,Hero hero,int provide){
		HeroInfo.Builder builder = HeroInfo.newBuilder();
		HeroProxy heroProxy = hero.getHeroProxy();
		builder.setCt(type);
		builder.setId(Long.toString(hero.getId()));
		builder.setCid(hero.getCid());
		builder.setLvl(hero.getLvl());
		builder.setExp(hero.getExp());
		builder.setAdvancedLvl(hero.getAdvancedLvl());
		builder.setQuality(hero.getHeroProxy().getQuality());
		builder.setProvide(provide);
		builder.setFightPower(heroProxy.getFightPower());
		// 属性封装
		Map<Integer, Integer> attr = hero.getHeroProxy().getAttr();
		encapsHeroAttr(builder, attr);
		encapsEquipmentInfo(builder, hero.getHeroProxy().getEquipment());
		
		PlayerData playerData = hero.getPlayerPkPlayerId().getPlayerDatasFkPlayerId();
		int helpFightHeroCid = playerData.getPlayerDataProxy().getDiscreteVO().getHelpFigntHeroCid();
		builder.setHelpFight(hero.getCid() == helpFightHeroCid);
		builder.setAngelLvl(hero.getAngelLvl());
		// 天使技能
		builder.addAllAngeSkillInfos(createAngeSkillInfo(hero));
		// 技能点使用情况
		builder.setUseSkillPiont(hero.getHeroProxy().getAlreadyUseSkillPiont());
		builder.setSkinId(String.valueOf(hero.getHeroProxy().getSkinVo().getId()));
		builder.setSkinCid(hero.getHeroProxy().getSkinVo().getCid());
		
		return builder.build();
	}

	private static void encapsEquipmentInfo(HeroInfo.Builder builder, Map<Integer, EquipmentVO> equipmentMap) {
		equipmentMap.forEach((position, equipment) -> {
			S2CHeroMsg.HeroEquipment.Builder equipmentBuilder = S2CHeroMsg.HeroEquipment.newBuilder();
			equipmentBuilder.setEquipmentId(Long.toString(equipment.getId()));
			equipmentBuilder.setPosition(position);
			builder.addEquipments(equipmentBuilder);
		});
	}

	/**
	 * 封装必要更新信息(非完整信息)
	 */
	public static HeroInfo createNecessaryHeroInfo(ChangeType type,Map<String,Object> info){
		HeroInfo.Builder builder = HeroInfo.newBuilder();
		builder.setCt(type);
		info.forEach((key,value)->{
			switch (key){
				case HERO_ID:
					builder.setId(value.toString());
					break;
				case HERO_CID:
					builder.setCid((Integer) value);
					break;
				case HERO_LVL:
					builder.setLvl((Integer) value);
					break;
				case HERO_EXP:
					builder.setExp((Long) value);
					break;
				case HERO_ADVANCED_LVL:
					builder.setAdvancedLvl((Integer) value);
					break;
				case HERO_ATTR:
					Map<Integer, Integer> attr = (Map<Integer, Integer>) value;
					encapsHeroAttr(builder, attr);
					break;
				case HERO_EQUIPMENT:
					Map<Integer,EquipmentVO> equipmentMap = (Map<Integer, EquipmentVO>) value;
					encapsEquipmentInfo(builder,equipmentMap);
					break;
				case HELP_FIGHT:
					builder.setHelpFight((boolean)value);
					break;
			}
		});
		return builder.build();
	}

	private static void encapsHeroAttr(HeroInfo.Builder builder, Map<Integer, Integer> attr) {
		AttributeInfo.Builder attrBuilder;
		for (Entry<Integer, Integer> entry : attr.entrySet()) {
            attrBuilder = AttributeInfo.newBuilder();
            attrBuilder.setType(entry.getKey());
            attrBuilder.setVal(entry.getValue());
            builder.addAttr(attrBuilder.build());
        }
	}

	public static HeroInfoList createHeroInfoList(ChangeType type,Collection<Hero> heros){
		HeroInfoList.Builder builder = HeroInfoList.newBuilder();
		for (Hero hero : heros) {
			builder.addHeros(createHeroInfo(type, hero));
		}
		return builder.build();
	}
	
	
	public static List<AngeSkillInfo> createAngeSkillInfo(Hero hero){
		List<AngeSkillInfo> list = new ArrayList<>();
		Map<String, Integer> angel = hero.getAngel();
		for (Entry<String, Integer> e : angel.entrySet()) {
			String[] key = e.getKey().split("_");
			int type = Integer.valueOf(key[0]);
			int pos = Integer.valueOf(key[1]);
			int val = e.getValue();
			AngeSkillInfo.Builder builder = AngeSkillInfo.newBuilder().setType(type).setPos(pos).setLvl(val);
			list.add(builder.build());
		}
		return list;
	}

	public static S2CHeroMsg.RespChangeHeroSkin createChangeHeroSkin(Hero hero, SkinVO beforeSkin){
		S2CHeroMsg.RespChangeHeroSkin.Builder builder = S2CHeroMsg.RespChangeHeroSkin.newBuilder();
		builder.setHero(createHeroInfo(ChangeType.UPDATE,hero));
		builder.setBeforeSkinId(beforeSkin.getId()+"");
		return builder.build();
	}
}
